<script>
export default {
  created () {
    // 调用API从本地缓存中获取数据
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    console.log('app created and cache logs by setStorageSync')
  },
  mounted () {
    wx.checkSession({
      success: function(res) {
        // session_key未过期，并且本生命周期一直有效
        console.log(res)
      },
      fail: function() {
        // session_key已经失效，需要重新登录
        wx.login({
          success: function(res) {
            console.log(res)
            if (res.code) {
              var code = res.code
              wx.setStorageSync("code", code)
              wx.request({
                url:"https://www.h7go.com/user/user/weiXinLoginTest?code=" +
                  code +
                  "&type=true",
                method: "post",
                success(data) {
                  console.log(data)
                  var appsecret = data.data.result.appsecret
                  var userId = data.data.result.userId
                  try {
                    wx.setStorageSync("appsecret", appsecret)
                    wx.setStorageSync("userId", userId)
                  } catch(e) {
                    console.error(e)
                  }
                },
                fail() {
                  wx.showToast({
                    title: "网站正在维护中...",
                    icon: "loading",
                    duration: 10000
                  })
                }
              })
            }
          }
        })
      }
    })
  }
}
</script>

<style>
page {
  background: #f0eff5;
}
.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 10rpx 0;
  box-sizing: border-box;
}
/* this rule will be remove */
* {
  transition: width 2s;
  -moz-transition: width 2s;
  -webkit-transition: width 2s;
  -o-transition: width 2s;
}
</style>
